package com.putong35.easyui.picture.picker;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;

import com.putong35.common.httprequest.responsecallbacks.StringResponseListener;
import com.putong35.common.qiniu.QiniuFileUploader;
import com.putong35.common.utils.EasyToast;

import static android.app.Activity.RESULT_OK;

/**
 * Created by Administrator on 2017/8/30.
 */

public class GetAudioAndUploadFragment extends Fragment{
    public static final String TAG = GetAudioAndUploadFragment.class.getSimpleName();
    public static final int REQ_CODE = 1000;
    ProgressDialog dialog;
    public GetAudioAndUploadFragment() {
        // Required empty public constructor

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialog = new ProgressDialog(getContext());
        dialog.setMessage("正在上传数据...");
    }

    public static GetAudioAndUploadFragment newInstance() {
        GetAudioAndUploadFragment fragment = new GetAudioAndUploadFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public void getVedio() {
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, REQ_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.e(TAG, "onActivityResult: ");
        if (requestCode == REQ_CODE && resultCode == RESULT_OK && null != data) {
            Uri selectedVideo = data.getData();
            String[] filePathColumn = {MediaStore.Audio.Media.DATA};

            Cursor cursor = getActivity().getContentResolver().query(selectedVideo,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

            String videoPath = cursor.getString(columnIndex);
            Log.e(TAG, "onActivityResult: videoPath " + videoPath);


            Uri uri = Uri.parse(videoPath);
            upload(uri.toString());
            cursor.close();

        }
    }

    private void upload(String path) {
        EasyToast.showToast(getActivity(), "开始上传数据");
        dialog.show();

        QiniuFileUploader.uploadFile(getContext(), path, new StringResponseListener() {
            @Override
            public void onSuccess(String url) {
               /* if (pictureReturnSuccessListener != null) {
                    pictureReturnSuccessListener.OnPictureReturnSuccess(null, url);
                }*/
                Log.e(TAG, "onSuccess: url == " + url);
                dialog.dismiss();
                EasyToast.showToast(getActivity(), "上传成功");
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Log.e(TAG, "onFailure: errorNo == " + errorNo + " message = " + strMsg);
               /* if (pictureReturnFailureListener != null) {
                    pictureReturnFailureListener.OnPictureReturnFailure(null, t, errorNo, strMsg);
                }*/
                dialog.dismiss();
                EasyToast.showToast(getActivity(), "上传失败");
            }
        });
    }

}
